from db_pool import DBDevice
from x_pc.db.base_model import Available


class CommandRouteStep(DBDevice, Available):
    """
    指令作业路径 步骤
    """
    # 编号
    id: str = None
    # 顺序
    seq: int = None
    # 所属工序路径 (关联CommandRoute.id)
    wops_command_routeid: str = None
    # 对应工艺
    wops_work_resourceid: str = None

    # (扩展字段) 资源类型
    ext_resource_type: int = None
    # (扩展字段) 设备ID 列表
    ext_equid_list: list[str] = []
    # (扩展字段) 设备类型 equ_type 仅GPT(人工拣选)和JXSGPT(机械手拣选)会出现大于1的情况
    ext_equ_type: list[int] = None
    ext_equ_type_str: list[str] = None
    # (扩展字段) 工作类型 work_type
    ext_work_type: int = None
    ext_work_type_str: str = None

    def __str__(self):
        return f'{self.__class__.__name__} (id={self.id}, seq={self.seq}, routeid={self.wops_command_routeid}, resourceid={self.wops_work_resourceid})'

    def __repr__(self):
        return self.__str__()

    def to_dict(self):
        return {
            'id': self.id,
            'seq': self.seq,
            'wops_command_routeid': self.wops_command_routeid,
            'wops_work_resourceid': self.wops_work_resourceid,
            'ext_resource_type': self.ext_resource_type,
            'ext_equid_list': self.ext_equid_list,
            'ext_equ_type': self.ext_equ_type,
            'ext_work_type': self.ext_work_type
        }


class CommandRoute(DBDevice, Available):
    """
    指令作业路径
    """
    # 编号
    id: str = None
    # 名称
    name: str = None
    # 指令类型
    command_type_code: str = None
    # 开始包装类型
    start_pack_type: int = None
    # 结束包装类型
    end_pack_type: int = None
    # 0否 1是
    is_mpq: str = None
    # LPN是否出过库，针对盘点使用
    has_out: int = None
    _remark: str | None = None
    _carry_type: str | None = None
    # 执行标记
    exeflag: str = None

    # (扩展字段) 路径 步骤 列表
    ext_step_list: list[CommandRouteStep] = []

    # (扩展字段) 标识 是否为 发货出货口
    ext_ch_port: int | None = None
    ext_ch_port_ids: list[str] | None = None
    ext_ch_port_codes: list[str] | None = None
    # (扩展自动) 标识 是否为 在库出货口 (可能有多个设备类型/出货口选择)
    ext_zk_ports: list[int] = None

    @property
    def remark(self) -> str | None:
        return self._remark

    @remark.setter
    def remark(self, data: str):
        if data and data.strip():
            self._remark = data.strip()
        else:
            self._remark = None

    @property
    def carry_type(self) -> str | None:
        return self._carry_type

    @carry_type.setter
    def carry_type(self, data: str):
        if data and data.strip():
            self._carry_type = data.strip()
        else:
            self._carry_type = None

    def is_zz(self):
        """
        是否为 自制路径
        """
        return self.remark and self.remark.startswith('ZZ-')

    def is_out(self) -> bool:
        """
        LPN是否出过库
        :return:
        """
        return self.has_out == 1

    def __str__(self):
        return f'{self.__class__.__name__} (id={self.id}, name={self.name})'

    def __repr__(self):
        return self.__str__()

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'command_type_code': self.command_type_code,
            'start_pack_type': self.start_pack_type,
            'end_pack_type': self.end_pack_type,
            'is_mpq': self.is_mpq,
            'has_out': self.has_out,
            'remark': self.remark,
            'ext_step_list': [_.to_dict() for _ in self.ext_step_list]
        }


class WorkResource(DBDevice):
    """
    资源组
    """
    # 工序编号
    id: str = None
    #
    name: str = None
    # 工作类型 关联wops_work_type
    work_type: int = None
    # 1工作中心 2工作单元 3搬运 4传送线
    resource_type: int = None

    def __str__(self):
        return f'{self.__class__.__name__} (id={self.id}, name={self.name}, work_type: {self.work_type}, resource_type: {self.resource_type})'


class ResourceEquRelation(DBDevice):
    """
    资源ID
    """
    wops_work_resourceid: str = None
    wops_equid: str = None

    def __str__(self):
        return f'{self.__class__.__name__} (resourceid={self.wops_work_resourceid}, equid={self.wops_equid})'

    def __repr__(self):
        return self.__str__()
